package com.gas.project.gas.morality.controller;

import java.util.List;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.poi.ExcelUtil;
import com.gas.framework.aspectj.lang.annotation.Log;
import com.gas.framework.aspectj.lang.enums.BusinessType;
import com.gas.framework.web.controller.BaseController;
import com.gas.framework.web.domain.AjaxResult;
import com.gas.framework.web.page.TableDataInfo;
import com.gas.project.gas.grade.domain.CourseValidGrade;
import com.gas.project.gas.message.domain.Message;
import com.gas.project.gas.message.service.IMessageService;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gas.project.gas.morality.domain.Morality;
import com.gas.project.gas.morality.service.IMoralityService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 奖惩信息Controller
 *
 * @author xyl
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/gas/morality")
public class MoralityController extends BaseController {
    private String prefix = "gas/morality";

    @Autowired
    private IMoralityService moralityService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IMessageService messageService;

    @RequiresPermissions("gas:morality:view")
    @GetMapping()
    public String morality() {
        return prefix + "/morality";
    }

    /**
     * 查询奖惩信息列表
     */
    @RequiresPermissions("gas:morality:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Morality morality) {
        User sysUser = getSysUser();
        if(sysUser.isStudent()){
            morality.setStudentNumber(sysUser.getLoginName());
        }
        if(sysUser.isTeacher()){
            morality.setMyStudent(sysUser.getLoginName());
        }
        startPage();
        List<Morality> list = moralityService.selectMoralityList(morality);
        for (Morality m : list) {
            User user = userService.selectUserByLoginName(m.getTeacherNumber());
            Message message = new Message();
            message.setModuleName("morality");
            message.setCourseNumber(m.getMoralityId() + "");
            message.setSubmitId(sysUser.getUserId());
            message.setReceiveId(user.getUserId());
            List<Message> messages = messageService.selectMessageList(message);
            m.setReply(messages.size() > 0);
        }
        return getDataTable(list);
    }

    /**
     * 导出奖惩信息列表
     */
    @RequiresPermissions("gas:morality:export")
    @Log(title = "奖惩信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Morality morality) {
        List<Morality> list = moralityService.selectMoralityList(morality);
        ExcelUtil<Morality> util = new ExcelUtil<Morality>(Morality.class);
        return util.exportExcel(list, "morality");
    }

    /**
     * 新增奖惩信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存奖惩信息
     */
    @RequiresPermissions("gas:morality:add")
    @Log(title = "奖惩信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Morality morality) {
        return toAjax(moralityService.insertMorality(morality));
    }

    /**
     * 修改奖惩信息
     */
    @GetMapping("/edit/{moralityId}")
    public String edit(@PathVariable("moralityId") Long moralityId, ModelMap mmap) {
        Morality morality = moralityService.selectMoralityById(moralityId);
        mmap.put("morality", morality);
        return prefix + "/edit";
    }

    /**
     * 修改保存奖惩信息
     */
    @RequiresPermissions("gas:morality:edit")
    @Log(title = "奖惩信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Morality morality) {
        return toAjax(moralityService.updateMorality(morality));
    }

    /**
     * 删除奖惩信息
     */
    @RequiresPermissions("gas:morality:remove")
    @Log(title = "奖惩信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(moralityService.deleteMoralityByIds(ids));
    }

    @RequiresPermissions("gas:morality:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<Morality> util = new ExcelUtil<>(Morality.class);
        return util.importTemplateExcel("奖惩信息");
    }


    @Log(title = "奖惩信息" , businessType = BusinessType.IMPORT)
    @RequiresPermissions("gas:morality:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Morality> util = new ExcelUtil<>(Morality.class);
        List<Morality> moralityList = util.importExcel(file.getInputStream());
        //数据校验
        for (Morality morality : moralityList) {
            morality.setCreateBy(getLoginName());
            morality.setCreateTime(DateUtils.getNowDate());
            String check = morality.check(morality);
            if(StringUtils.isNotEmpty(check)){
                return error(check);
            }
        }
        String message = moralityService.importMorality(moralityList);
        return AjaxResult.success(message);
    }
}
